package main

import (
	"io"
	"log"
	"os"
	"time"
)

var (
	Debug   *log.Logger // 记录所有日志
	Info    *log.Logger // 重要的信息
	Warning *log.Logger // 需要注意的信息
	Error   *log.Logger // 非常严重的问题
)

func logger_init() {
	locale, err := time.LoadLocation("UTC")
	if err != nil {
		panic(err)
	}
	file, err := os.OpenFile(conf.LogFile+"_"+time.Now().Add(8*time.Hour).In(locale).Format("20060102-150405"),
		os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalln("Failed to open error log file:", err)
	}

	if conf.LogLevel == 0 {
		Debug = log.New(io.MultiWriter(file, os.Stdout),
			"DEBUG: ",
			log.Ldate|log.Ltime|log.Lshortfile)
	} else {
		Debug = log.New(os.Stdout,
			"DEBUG: ",
			log.Ldate|log.Ltime|log.Lshortfile)
	}

	if conf.LogLevel <= 1 {
		Info = log.New(io.MultiWriter(file, os.Stdout),
			"INFO: ",
			log.Ldate|log.Ltime|log.Lshortfile)
	} else {
		Info = log.New(os.Stdout,
			"INFO: ",
			log.Ldate|log.Ltime|log.Lshortfile)
	}

	if conf.LogLevel <= 2 {
		Warning = log.New(io.MultiWriter(file, os.Stdout),
			"WARNING: ",
			log.Ldate|log.Ltime|log.Lshortfile)
	} else {
		Warning = log.New(os.Stdout,
			"WARNING: ",
			log.Ldate|log.Ltime|log.Lshortfile)
	}

	Error = log.New(io.MultiWriter(file, os.Stderr),
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)
}
